﻿using BL.Models.Account;
using HitMapsNew.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using UIServices;

namespace HitMapsNew.Controllers
{
    [AuthorizeUserOnly]
    public class AccountController : Controller
    {
        private User loggedInCustomer
        {
            get
            {
                return UIServices.FacadeServices.Instance.AccountManagerService.GetCurrentUser();
            }
        }


        [AllowAnonymous]
        public ActionResult Login(string error = "")
        {
            return this.View(new UserLoginModel(error));
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        [AllowAnonymous]
        public ActionResult Login(UserLoginModel user)
        {
            if (ModelState.IsValid)
            {
                AccountManager.eCredentialsStatus credentialsStatus = UIServices.FacadeServices.Instance.AccountManagerService.LogIn(user.UserName, user.Password, user.StayConnected);

                switch (credentialsStatus)
                {
                    case AccountManager.eCredentialsStatus.Ok:
                        return RedirectToAction("Menus", "EditData");
                    case AccountManager.eCredentialsStatus.UserIsNotAuntenicated:
                        return Login("המשתמש אינו מאומת");
                    case AccountManager.eCredentialsStatus.PasswordIsNotOk:
                        return Login("פרטי ההתחברות אינם נכונים");
                    case AccountManager.eCredentialsStatus.UserNotFound:
                        return Login("היוזר לא נמצא");
                }
            }
            
            return Login("הפרטים אינם נכונים");
        }

        [HttpGet]
        [AllowAnonymous]
        public ActionResult Register(string message = "", bool isRegCompleted = false)
        {
            return this.View(new UserRegistrationModel() { Message = message, IsRegistrationCompleted = isRegCompleted });
        }

        [AllowAnonymous]
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Register(UserRegistrationModel user)
        {
            if (ModelState.IsValid && user.Password == user.ReTypePassword)
            {

                var registrationUser = new User
                {
                    UserName = user.UserName,
                    IsAuturize = false
                };


                if (UIServices.FacadeServices.Instance.AccountManagerService.RegisterUser(registrationUser, user.Password))
                {
                    return Register("אנא המתן לאימות מנהל", true);
                }
            }

            return Register("ההרשמה אינה תקינה");
        }


        [HttpGet]
        public ActionResult Logoff()
        {
            UIServices.FacadeServices.Instance.AccountManagerService.LogOut();
            return RedirectToAction("Index", "Home");
        }

    }

}
